Imports System.Windows.Forms
Imports VolumeRegistrationNamespace
Imports twoDMatrixLibrary
Public Class RegistrationForm
    Dim _nlmobject As VolumeRegistrationNamespace.VolumeRegistrationByMutualInformation
    Dim _a, _f As MatrixDataStructures.Matrix3DInt16

    Private Function CheckInputs() As Boolean
        Dim ti As Integer, ts As Single
        If Single.TryParse(xr1.Text, ts) = False Then Return False
        If Single.TryParse(xr2.Text, ts) = False Then Return False
        If Single.TryParse(xr3.Text, ts) = False Then Return False
        If Single.TryParse(yr1.Text, ts) = False Then Return False
        If Single.TryParse(yr2.Text, ts) = False Then Return False
        If Single.TryParse(yr3.Text, ts) = False Then Return False
        If Single.TryParse(zr1.Text, ts) = False Then Return False
        If Single.TryParse(zr2.Text, ts) = False Then Return False
        If Single.TryParse(zr3.Text, ts) = False Then Return False
        If Single.TryParse(xt1.Text, ts) = False Then Return False
        If Single.TryParse(xt2.Text, ts) = False Then Return False
        If Single.TryParse(xt3.Text, ts) = False Then Return False
        If Single.TryParse(yt1.Text, ts) = False Then Return False
        If Single.TryParse(yt2.Text, ts) = False Then Return False
        If Single.TryParse(yt3.Text, ts) = False Then Return False
        If Single.TryParse(zt1.Text, ts) = False Then Return False
        If Single.TryParse(zt2.Text, ts) = False Then Return False
        If Single.TryParse(zt3.Text, ts) = False Then Return False
        If Single.TryParse(xrmax.Text, ts) = False Then Return False
        If Single.TryParse(yrmax.Text, ts) = False Then Return False
        If Single.TryParse(zrmax.Text, ts) = False Then Return False
        If Single.TryParse(xtmax.Text, ts) = False Then Return False
        If Single.TryParse(ytmax.Text, ts) = False Then Return False
        If Single.TryParse(ztmax.Text, ts) = False Then Return False
        If Single.TryParse(rc.Text, ts) = False Then Return False
        If Single.TryParse(ec.Text, ts) = False Then Return False
        If Single.TryParse(cc.Text, ts) = False Then Return False
        If Single.TryParse(sc.Text, ts) = False Then Return False
        If Integer.TryParse(itmax.Text, ti) = False Then Return False
        If Integer.TryParse(rscsize.Text, ti) = False Then Return False

        Return True
    End Function

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        OK_Button.Enabled = False
        logbox.AppendText(vbNewLine & "Parsing parameter values ....")
        'Check for Singles and Integer Values
        If CheckInputs() = True Then
            logbox.AppendText("Done.")
        Else
            logbox.AppendText("Failed.")
            OK_Button.Enabled = True
            Exit Sub
        End If
        
        'Form Vectors
        logbox.AppendText(vbNewLine & "Forming Vectors..")
        Dim v1 As New twoDMatrixLibrary.Vector(6)
        v1(0) = Single.Parse(xr1.Text)
        v1(1) = Single.Parse(yr1.Text)
        v1(2) = Single.Parse(zr1.Text)
        v1(3) = Single.Parse(xt1.Text)
        v1(4) = Single.Parse(yt1.Text)
        v1(5) = Single.Parse(zt1.Text)
        Dim v2 As New twoDMatrixLibrary.Vector(6)
        v2(0) = Single.Parse(xr2.Text)
        v2(1) = Single.Parse(yr2.Text)
        v2(2) = Single.Parse(zr2.Text)
        v2(3) = Single.Parse(xt2.Text)
        v2(4) = Single.Parse(yt2.Text)
        v2(5) = Single.Parse(zt2.Text)
        Dim v3 As New twoDMatrixLibrary.Vector(6)
        v3(0) = Single.Parse(xr3.Text)
        v3(1) = Single.Parse(yr3.Text)
        v3(2) = Single.Parse(zr3.Text)
        v3(3) = Single.Parse(xt3.Text)
        v3(4) = Single.Parse(yt3.Text)
        v3(5) = Single.Parse(zt3.Text)

        Dim max As New twoDMatrixLibrary.Vector(6)
        max(0) = Single.Parse(xrmax.Text)
        max(1) = Single.Parse(yrmax.Text)
        max(2) = Single.Parse(zrmax.Text)
        max(3) = Single.Parse(xtmax.Text)
        max(4) = Single.Parse(ytmax.Text)
        max(5) = Single.Parse(ztmax.Text)

        logbox.AppendText("Done.")
        logbox.AppendText(vbNewLine & "Vector 1:" & v1.ToString)
        logbox.AppendText(vbNewLine & "Vector 2:" & v2.ToString)
        logbox.AppendText(vbNewLine & "Vector 3:" & v3.ToString)
        logbox.AppendText(vbNewLine & "Vector MaxValues:" & max.ToString)

        'Rescale
        logbox.AppendText(vbNewLine & "Rescaling Matrices ... ")

        _a = _a.ReScale(_a, 0, Integer.Parse(rscsize.Text))
        _f = _f.ReScale(_f, 0, Integer.Parse(rscsize.Text))
        logbox.AppendText("Done. ")

        ' Dim point As Vector = temp.ComputeMinimum(0, v1, v2, v3, max, 10000, AnatomyVolume.Matrix, CType(FAVolume.MatrixD.ReScale(FAVolume.MatrixD, 0, 64, True), Matrix3DInt16))

        If MsgBox("Annealing?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            logbox.AppendText(vbNewLine & "Initializing Simulated Annealing ...")
            _nlmobject = New VolumeRegistrationByMutualInformation(_a.x, _a.y, _a.z)
            logbox.AppendText("Done.")

            'Compute Minimum
            Dim temp As twoDMatrixLibrary.Vector = _nlmobject.ComputeAnnealingMinimum(max, _a, _f, CSng(Me.txtTempStart.Text), CInt(Me.txtIterationsMonteCarlo.Text), CSng(Me.txtTempDecrease.Text), CSng(Me.txtTempStop.Text))

            logbox.AppendText(vbNewLine & "Details ..." & temp.ToString & ".")

        Else
            'Initialize NMS Search Class
            logbox.AppendText(vbNewLine & "Initializing Nelder Mead Search ...")
            _nlmobject = New VolumeRegistrationByMutualInformation(_a.x, _a.y, _a.z, Double.Parse(rc.Text), Double.Parse(ec.Text), Double.Parse(cc.Text), Double.Parse(sc.Text))
            logbox.AppendText("Done. Parameters set : " & _nlmobject.PrintParameters & ".")

            'Compute Minimum
            _nlmobject.ComputeMinimum(v1, v2, v3, max, Integer.Parse(itmax.Text), _a, _f)

            logbox.AppendText(vbNewLine & "Details ...")
            Dim Log As ArrayList = _nlmobject.Log
            For Each A() As String In Log
                logbox.AppendText(vbNewLine & A(0) & ", MI: " & A(1) & ".")
            Next

        End If
        logbox.AppendText("Done.")
        Me.Button1.Enabled = True
        OK_Button.Enabled = True
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Public Event completed()
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RaiseEvent completed()
    End Sub

    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub
    Sub New(ByVal StaticMatrix As MatrixDataStructures.Matrix3DInt16, ByVal DynamicMatrix As MatrixDataStructures.Matrix3DInt16)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me._a = StaticMatrix
        Me._f = DynamicMatrix
    End Sub
    Sub New(ByVal StaticMatrix As MatrixDataStructures.Matrix3DInt16, ByVal DynamicMatrix As MatrixDataStructures.Matrix3DSingle)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me._a = StaticMatrix
        Me._f = DynamicMatrix.ReScaleToINT(DynamicMatrix, DynamicMatrix.Minimum, DynamicMatrix.Maximum)
    End Sub
End Class
